package pl.diagnoser.server.persistance.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import pl.diagnoser.client.dto.DefectDictDTO;
import pl.diagnoser.server.persistance.map.DefectDict;
import pl.diagnoser.server.persistance.tools.HibernateUtil;

public class DefectDictDao implements Serializable {

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        public List<DefectDictDTO> getDefectsList() {
                
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
                List<DefectDict> defectsList = new ArrayList<DefectDict>(session.createQuery("from DefectDict").list());
                List<DefectDictDTO> defectsListDTO = new ArrayList<DefectDictDTO>(defectsList != null ? defectsList.size() : 0);
                
                if(defectsList != null) {
                        for (DefectDict defect : defectsList) {
                                defectsListDTO.add(createDefectDictDTO(defect));
                        }
                }
                session.getTransaction().commit();
                session.close();
                return defectsListDTO;
        }
        
        @SuppressWarnings("unchecked")
        public List<String> getDefectsListAsString() {
                
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
                List<DefectDict> defectsList = new ArrayList<DefectDict>(session.createQuery("from DefectDict").list());
                List<String> defectListString = new ArrayList<String>(defectsList != null ? defectsList.size() : 0);
                if(defectsList != null) {
                        for (DefectDict defect : defectsList) {
                                defectListString.add(defect.getDefectName());
                        }
                }
                
                session.getTransaction().commit();
                session.close();
                return defectListString;
        }
        
        public String saveDefect(String newValue) {
                String ret = "OK";
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
            try {
                session.save(new DefectDict(newValue));
                session.getTransaction().commit();
            } catch(Exception e) {
                ret = e.getMessage();
            } finally {
                        session.close();
            }
                return ret;
        }
        
        public String removeDefects(List<String> defects) {
                String ret = "OK";
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
            try {
                for(String defectName : defects) {
                        DefectDict defect = (DefectDict) session.createCriteria(DefectDict.class).add(Expression.eq("defectName", defectName)).uniqueResult();
                        session.delete(defect);
                        session.getTransaction().commit();
                }
            } catch(Exception e) {
                ret = e.getMessage();
            } finally {
                        session.close();
            }   
                return ret;
        }
        
        public DefectDictDTO createDefectDictDTO( DefectDict defectDict ) {
                return new DefectDictDTO( defectDict.getDefectName(), defectDict.getDefectConfidence() );
        }
}
